home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / xdg / BaseDirectory.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  4.3 KB  |  106 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''
  5. This module is based on a rox module (LGPL):
  6.  
  7. http://cvs.sourceforge.net/viewcvs.py/rox/ROX-Lib2/python/rox/basedir.py?rev=1.9&view=log
  8.  
  9. The freedesktop.org Base Directory specification provides a way for
  10. applications to locate shared data and configuration:
  11.  
  12. \thttp://standards.freedesktop.org/basedir-spec/
  13.  
  14. (based on version 0.6)
  15.  
  16. This module can be used to load and save from and to these directories.
  17.  
  18. Typical usage:
  19.  
  20. \tfrom rox import basedir
  21. \t
  22. \tfor dir in basedir.load_config_paths(\'mydomain.org\', \'MyProg\', \'Options\'):
  23. \t\tprint "Load settings from", dir
  24.  
  25. \tdir = basedir.save_config_path(\'mydomain.org\', \'MyProg\')
  26. \tprint >>file(os.path.join(dir, \'Options\'), \'w\'), "foo=2"
  27.  
  28. Note: see the rox.Options module for a higher-level API for managing options.
  29. '''
  30. from __future__ import generators
  31. import os
  32. _home = os.environ.get('HOME', '/')
  33. xdg_data_home = os.environ.get('XDG_DATA_HOME', os.path.join(_home, '.local', 'share'))
  34. xdg_data_dirs = [
  35.     xdg_data_home] + os.environ.get('XDG_DATA_DIRS', '/usr/local/share:/usr/share').split(':')
  36. xdg_config_home = os.environ.get('XDG_CONFIG_HOME', os.path.join(_home, '.config'))
  37. xdg_config_dirs = [
  38.     xdg_config_home] + os.environ.get('XDG_CONFIG_DIRS', '/etc/xdg').split(':')
  39. xdg_cache_home = os.environ.get('XDG_CACHE_HOME', os.path.join(_home, '.cache'))
  40. xdg_data_dirs = filter((lambda x: x), xdg_data_dirs)
  41. xdg_config_dirs = filter((lambda x: x), xdg_config_dirs)
  42.  
  43. def save_config_path(*resource):
  44.     """Ensure $XDG_CONFIG_HOME/<resource>/ exists, and return its path.
  45. \t'resource' should normally be the name of your application. Use this
  46. \twhen SAVING configuration settings. Use the xdg_config_dirs variable
  47. \tfor loading."""
  48.     resource = os.path.join(*resource)
  49.     if not not resource.startswith('/'):
  50.         raise AssertionError
  51.     path = os.path.join(xdg_config_home, resource)
  52.     if not os.path.isdir(path):
  53.         os.makedirs(path, 448)
  54.     
  55.     return path
  56.  
  57.  
  58. def save_data_path(*resource):
  59.     """Ensure $XDG_DATA_HOME/<resource>/ exists, and return its path.
  60. \t'resource' is the name of some shared resource. Use this when updating
  61. \ta shared (between programs) database. Use the xdg_data_dirs variable
  62. \tfor loading."""
  63.     resource = os.path.join(*resource)
  64.     if not not resource.startswith('/'):
  65.         raise AssertionError
  66.     path = os.path.join(xdg_data_home, resource)
  67.     if not os.path.isdir(path):
  68.         os.makedirs(path)
  69.     
  70.     return path
  71.  
  72.  
  73. def load_config_paths(*resource):
  74.     """Returns an iterator which gives each directory named 'resource' in the
  75. \tconfiguration search path. Information provided by earlier directories should
  76. \ttake precedence over later ones (ie, the user's config dir comes first)."""
  77.     resource = os.path.join(*resource)
  78.     for config_dir in xdg_config_dirs:
  79.         path = os.path.join(config_dir, resource)
  80.         if os.path.exists(path):
  81.             yield path
  82.             continue
  83.     
  84.  
  85.  
  86. def load_first_config(*resource):
  87.     '''Returns the first result from load_config_paths, or None if there is nothing
  88. \tto load.'''
  89.     for x in load_config_paths(*resource):
  90.         return x
  91.     
  92.  
  93.  
  94. def load_data_paths(*resource):
  95.     """Returns an iterator which gives each directory named 'resource' in the
  96. \tshared data search path. Information provided by earlier directories should
  97. \ttake precedence over later ones."""
  98.     resource = os.path.join(*resource)
  99.     for data_dir in xdg_data_dirs:
  100.         path = os.path.join(data_dir, resource)
  101.         if os.path.exists(path):
  102.             yield path
  103.             continue
  104.     
  105.  
  106.